package cn.acewill.yxd.yunxundian.pubClass;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.DbManager;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.adapter.ListitemAdapter;

public class multiSelect extends AppCompatActivity {
    private YXDAPP yxdapp=new YXDAPP();
    private DbManager db = x.getDb(yxdapp.getDaoConfig());
    private List<SimpleSpinnerOption> lst= new ArrayList<SimpleSpinnerOption>();
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.muti_select_ok, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_select);
        Intent getDate=getIntent();
        String titleStr=getDate.getStringExtra("title");
        String lstJson=getDate.getStringExtra("lstData");
        final String xuanzeMode=getDate.getStringExtra("xuanze");
        final String daima=getDate.getStringExtra("daima");
        Gson gson=new Gson();
        Type typeList = new TypeToken<ArrayList<SimpleSpinnerOption>>() {
        }.getType();
        lst = gson.fromJson(pubClass.convertToArrayJsonNormal(lstJson), typeList);
        Toolbar toolbar = (Toolbar) findViewById(R.id.mutb);
        toolbar.setTitle(titleStr);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.action_ok:
                    {
                        Intent intent=new Intent();
                        intent.putExtra("result",JSON.toJSONString(lst));
                        intent.putExtra("daima",daima);
                        setResult(1,intent);
                        finish();
                    }
                }

                return false;
            }
        });

        //ListitemAdapter adapter = new ListitemAdapter(this, lst);
        //final ListView listView = (ListView)findViewById(R.id.muli);
        //listView.setAdapter(adapter);


        //构造Adapter
        final ListitemAdapter adapter = new ListitemAdapter(multiSelect.this,lst);

        //设置adapter
        final ListView listView = (ListView) findViewById(R.id.muli);
        listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                ListView listView = (ListView) parent;
                SimpleSpinnerOption map = (SimpleSpinnerOption) listView.getItemAtPosition(position);
                if(xuanzeMode.equals("duo")) {
                    boolean checked = map.isChecked();
                    if (!checked) {
                        map.setChecked(true);
                    } else {
                        map.setChecked(false);
                    }
                }
                else{
                    for(SimpleSpinnerOption ss:lst)
                        ss.setChecked(false);
                    map.setChecked(true);
                }
                adapter.notifyDataSetChanged();
            }
        });

        final EditText ft=(EditText)findViewById(R.id.fliterTxt);
        ft.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                adapter.getFilter().filter(ft.getText().toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }



}
